home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / recio212.zip / rcputs.c < prev    next >
C/C++ Source or Header  |  1995-01-29  |  4KB  |  98 lines

  1. /*****************************************************************************
  2.    MODULE: rcputs.c
  3.   PURPOSE: recio column delimited string and char output functions
  4. COPYRIGHT: (C) 1994-1995, William Pierpoint
  5.  COMPILER: Borland C Version 3.1
  6.        OS: MSDOS Version 6.2
  7.   VERSION: 2.12
  8.   RELEASE: January 29, 1995
  9. *****************************************************************************/
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14.  
  15. #include "recio.h"
  16.  
  17. #define rcol(rp)         ((rp)->r_colno)
  18. #define rfp(rp)          ((rp)->r_fp)
  19. #define rflags(rp)       ((rp)->r_flags)
  20. #define rfldch(rp)       ((rp)->r_fldch)
  21. #define rtxtch(rp)       ((rp)->r_txtch)
  22. #define rflags(rp)       ((rp)->r_flags)
  23.  
  24. extern int _risready(REC *rp, int mode);
  25. extern int _rputc(REC *rp, int ch);
  26.  
  27. /****************************************************************************/
  28. void                         /* returns nothing                             */
  29.     rcputc(                  /* put character to record stream              */
  30.         REC   *rp,           /* record pointer                              */
  31.         size_t col,          /* column position                             */
  32.         int    ch)           /* character to put to record stream           */
  33. /****************************************************************************/
  34. {
  35.     if (_risready(rp, R_WRITE)) {
  36.         if (col >= rcolno(rp)) {
  37.             rfldno(rp)++;
  38.             rflags(rp) &= ~_R_TXT;
  39.             
  40.             /* if colno < col, pad with spaces */
  41.             while (rcolno(rp) < col) {
  42.                 if (_rputc(rp, ' ')) goto done;
  43.             }
  44.             
  45.             /* put character */
  46.             _rputc(rp, ch);
  47.         } else {
  48.             rseterr(rp, R_EINVAL); \
  49.         }
  50.     }
  51. done:
  52. }
  53.  
  54. /****************************************************************************/
  55. void                         /* returns nothing                             */
  56.     rcputs(                  /* put string to record stream                 */
  57.         REC  *rp,            /* record pointer                              */
  58.         size_t begcol,       /* beginning column                            */
  59.         size_t endcol,       /* ending column                               */
  60.   const char *str)           /* pointer to string                           */
  61. /****************************************************************************/
  62. /* note: assumes str does not contain a newline character */
  63. {
  64.     size_t sl;               /* string length */
  65.  
  66.     if (_risready(rp, R_WRITE)) {
  67.         if (endcol >= begcol && begcol >= rcolno(rp)) {
  68.             rfldno(rp)++;
  69.             rflags(rp) &= ~_R_TXT;
  70.  
  71.             /* if colno < begcol, pad with spaces */
  72.             while (rcolno(rp) < begcol) {
  73.                 if (_rputc(rp, ' ')) goto done;
  74.             }
  75.  
  76.             sl = strlen(str);
  77.  
  78.             /* if str fits within the space, output it */
  79.             if (sl <= (endcol-begcol+1)) {
  80.                 if (fputs(str, rfp(rp)) != EOF) { 
  81.                     rcol(rp) += sl;
  82.                 } else {
  83.                     rseterr(rp, R_ENOPUT);
  84.                 }
  85.             /* else put as much of the string as possible */
  86.             } else {
  87.                 rsetwarn(rp, R_WWIDTH);
  88.                 while (rcolno(rp) <= endcol) {
  89.                     if (_rputc(rp, *str++)) goto done;
  90.                 }
  91.             }
  92.         } else {
  93.             rseterr(rp, R_EINVAL); \
  94.         }
  95.     }
  96. done:
  97. }
  98.